<template>
  <view class="container">
    <!-- 顶部背景和导航 - 固定定位 -->
    <view class="fixed-header">
      <image src="/static/image/图层 1.png" class="bg-image"></image>
      <view class="title">
        <view class="back-arrow" @click="goBack"></view>
        <view>添加车辆</view>
        <view></view>
      </view>
    </view>
    
    <!-- 页面内容区域 -->
    <view class="page-content">
      <!-- 表单区域 -->
      <view class="form">
        <!-- 车辆类型 -->
        <view class="form-item">
          <text class="label">车辆类型</text>
          <picker @change="onTypeChange" :value="typeIndex" :range="typeOptions" class="picker">
            <view class="picker-text" :class="{ placeholder: !formData.type }">
              {{ formData.type || '请选择' }}
            </view>
          </picker>
        </view>

        <!-- 车辆型号 -->
        <view class="form-item">
          <text class="label">车辆型号</text>
          <picker @change="onModelChange" :value="modelIndex" :range="modelOptions" class="picker">
            <view class="picker-text" :class="{ placeholder: !formData.model }">
              {{ formData.model || '请选择' }}
            </view>
          </picker>
        </view>

        <!-- 车牌号码 -->
        <view class="form-item">
          <text class="label">车牌号码</text>
          <input 
            v-model="formData.plate" 
            placeholder="请输入您的车牌号" 
            maxlength="10"
            class="input"
          />
        </view>

        <!-- 车主姓名 -->
        <view class="form-item">
          <text class="label">车主姓名</text>
          <input 
            v-model="formData.owner" 
            placeholder="请输入您的姓名" 
            class="input"
          />
        </view>

        <!-- 电话号码 -->
        <view class="form-item">
          <text class="label">电话号码</text>
          <input 
            v-model="formData.phone" 
            placeholder="请输入您的手机号" 
            type="number"
            maxlength="11"
            class="input"
          />
        </view>

        <!-- 照片上传 -->
        <view class="form-item">
          <text class="label">照片</text>
          <view class="photo-upload" @click="uploadPhoto">
            <image v-if="formData.photo" :src="formData.photo" class="photo-preview"></image>
            <view v-else class="photo-placeholder">
              <text class="upload-icon">📷</text>
              <text class="upload-text">上传车辆照片</text>
            </view>
          </view>
        </view>

        <!-- 添加按钮 -->
        <view class="submit-btn" @click="submitForm" :class="{ disabled: !isFormValid }">
          <text class="add-icon">+</text>
          <text>添加车辆</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, reactive } from 'vue'

// 表单数据
const formData = reactive({
  type: '',
  model: '',
  plate: '',
  owner: '',
  phone: '',
  photo: ''
})

// 选择器数据
const typeOptions = ['轿车', 'SUV', 'MPV', '跑车', '货车', '客车']
const modelOptions = ['奔驰-GR', '宝马-X5', '奥迪-A6', '大众-帕萨特', '丰田-凯美瑞', '本田-雅阁']
const typeIndex = ref(-1)
const modelIndex = ref(-1)

// 表单验证
const isFormValid = computed(() => {
  return formData.type && formData.model && formData.plate && 
         formData.owner && formData.phone
})

// 返回上一页
const goBack = () => {
  uni.navigateBack()
}

// 车辆类型选择
const onTypeChange = (e) => {
  const index = e.detail.value
  typeIndex.value = index
  formData.type = typeOptions[index]
}

// 车辆型号选择
const onModelChange = (e) => {
  const index = e.detail.value
  modelIndex.value = index
  formData.model = modelOptions[index]
}

// 上传照片
const uploadPhoto = () => {
  uni.chooseImage({
    count: 1,
    sizeType: ['compressed'],
    sourceType: ['album', 'camera'],
    success: (res) => {
      formData.photo = res.tempFilePaths[0]
    }
  })
}

// 提交表单
const submitForm = () => {
  if (!isFormValid.value) {
    uni.showToast({
      title: '请填写完整信息',
      icon: 'none',
      duration: 2000
    })
    return
  }

  // 验证车牌号格式（简单验证）
  const plateRegex = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼][A-Z][A-Z0-9]{5}$/
  if (!plateRegex.test(formData.plate)) {
    uni.showToast({
      title: '请输入正确的车牌号',
      icon: 'none',
      duration: 2000
    })
    return
  }

  // 验证手机号格式
  const phoneRegex = /^1[3-9]\d{9}$/
  if (!phoneRegex.test(formData.phone)) {
    uni.showToast({
      title: '请输入正确的手机号',
      icon: 'none',
      duration: 2000
    })
    return
  }

  // 保存车辆信息（这里应该调用API）
  console.log('保存车辆信息:', formData)
  
  // 跳转到添加成功页面
  uni.navigateTo({
    url: '/pages/my/mycar/addsuccess/addsuccess'
  })
}
</script>

<style scoped>
.container {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
}

/* 固定头部区域 */
.fixed-header {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  height: 200px;
}

.bg-image {
  width: 100%;
  height: 200px;
}

.title {
  padding: 15px;
  width: 100%;
  display: flex;
  box-sizing: border-box;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1001;
  justify-content: space-between;
  align-items: center;
}

.title view:nth-child(2) {
  color: #fff;
  font-size: 18px;
  font-weight: 500;
}

.title view:nth-child(3) {
  width: 24px;
  height: 24px;
}

.back-arrow {
  width: 40px;
  height: 40px;
  position: relative;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-arrow::before {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  border-top: 2px solid #fff;
  border-left: 2px solid #fff;
  transform: rotate(-45deg);
  top: 50%;
  left: 50%;
  margin-top: -6px;
  margin-left: -6px;
  transition: all 0.3s ease;
}

/* 页面内容区域 */
.page-content {
  width: 100%;
  margin-top: 200px;
  position: relative;
  z-index: 10;
  min-height: calc(100vh - 200px);
  overflow-y: auto;
}

/* 表单区域 */
.form {
  padding: 20px 15px 30px;
  background-color: #fff;
  margin: 15px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.form-item {
  margin-bottom: 25px;
}

.label {
  display: block;
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
  font-weight: 500;
}

.picker {
  width: 100%;
}

.picker-text {
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  color: #333;
  background-color: #fff;
  transition: all 0.3s ease;
}

.picker-text:active {
  background-color: #f5f5f5;
  transform: scale(0.98);
}

.placeholder {
  color: #999;
}

.input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.input:focus {
  border-color: #007AFF;
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.1);
}

.photo-upload {
  width: 100%;
  height: 120px;
  border: 1px dashed #ddd;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f9f9f9;
  cursor: pointer;
  transition: all 0.3s ease;
}

.photo-upload:active {
  background-color: #f0f0f0;
  transform: scale(0.98);
}

.photo-preview {
  width: 100%;
  height: 100%;
  border-radius: 8px;
}

.photo-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.upload-icon {
  font-size: 24px;
}

.upload-text {
  font-size: 14px;
  color: #999;
}

.submit-btn {
  background-color: #007AFF;
  color: #fff;
  border-radius: 25px;
  padding: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 500;
  margin-top: 30px;
  transition: all 0.3s ease;
}

.submit-btn:active {
  background-color: #0056CC;
  transform: scale(0.98);
}

.submit-btn.disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.submit-btn.disabled:active {
  transform: none;
  background-color: #ccc;
}

.add-icon {
  font-size: 20px;
  font-weight: bold;
}
</style>